package com.victor.common.core.utils;

import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Iterator;

public class PdfCompressorUtil {

    public static boolean  pdfCompressor(String originalFile,String compressedFile,float f) {
        File inputFile = new File(originalFile);
         try {
             PDDocument document = PDDocument.load(inputFile);

             for (PDPage page : document.getPages()) {
                 PDResources resources = page.getResources();
                 for (COSName xObjectName : resources.getXObjectNames()) {
                     if (resources.isImageXObject(xObjectName)) {
                         PDImageXObject imageXObject = (PDImageXObject) resources.getXObject(xObjectName);

                         // 获取图像为BufferedImage
                         BufferedImage bufferedImage = imageXObject.getImage();
                         if (bufferedImage != null) {
                             // 创建输出流，用于保存压缩后的图像
                             ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

                             // 获取 JPEG 图像写入器
                             Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
                             ImageWriter writer = writers.next();

                             // 创建 ImageOutputStream
                             ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream);
                             writer.setOutput(ios);

                             // 设置压缩参数
                             ImageWriteParam param = writer.getDefaultWriteParam();
                             if (param.canWriteCompressed()) {
                                 param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                                 param.setCompressionQuality(f); // 质量参数 (0.0 - 1.0)，0.5表示中等压缩
                             }

                             // 写入压缩后的图像
                             writer.write(null, new IIOImage(bufferedImage, null, null), param);
                             writer.dispose(); // 释放资源

                             // 使用压缩后的图像数据创建新的PDImageXObject
                             PDImageXObject compressedImage = PDImageXObject.createFromByteArray(document, outputStream.toByteArray(), "compressed_image.jpg");

                             // 将压缩后的图像替换原来的图像对象
                             resources.put(xObjectName, compressedImage);
                         }

                     }
                 }
             }

             document.save(compressedFile);
             document.close();

             return  true;
         }catch (Exception e){
             throw new RuntimeException("压缩pdf中的图片异常",e);
         }

    }
}
